//
//  AroudTeachersViewController.m
//  jiyujiaProgram
//
//  Created by 中良 赵 on 15/12/6.
//  Copyright © 2015年 中良 赵. All rights reserved.
//

#import "AroudTeachersViewController.h"
#import "AroudTeachersTableViewCell.h"
#import "RankModel.h"
#import "InfoViewController.h"


@interface AroudTeachersViewController ()
{
    int pageIndex ;
    int pageSize ;
    NSMutableArray *_dataArray;
    RankModel *rankModel;

}
@end

@implementation AroudTeachersViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    pageIndex=0;
    pageSize=20;
    _tableview.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRefreshing)];
    _tableview.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefreshing)];

    [_tableview.mj_header beginRefreshing];
 
}
- (void)creatThirdHttpRequest
{
    UserInfoManager *userInfo = [UserInfoManager sharedUserInfoManager];
    [userInfo loadUserInfoFromSanbox];
    if (userInfo.lat.length >0 &&userInfo.lng.length > 0) {
        
        NSDictionary *param = @{
                                @"area":@"",
                                @"pageSize":[NSString stringWithFormat:@"%d",pageSize],
                                @"pageIndex":[NSString stringWithFormat:@"%d",pageIndex],
                                @"lng":userInfo.lng,
                                @"lat":userInfo.lat,
                                };
        //        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:param options:NSJSONWritingPrettyPrinted error:nil];
        //        NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        [[HttpManager defaultManager] postRequestToUrl:RequestURL(QUERYTEACHERDESC) params:param complete:^(BOOL successed, NSDictionary *result) {
            NSLog(@"%@",result);
            if (successed) {
                [RankModel setupObjectClassInArray:^NSDictionary *{
                    return @{
                             @"data":@"RankData",
                             };
                }];
//                _dataArray=[[NSMutableArray alloc]init];
//                
//                rankModel = [[RankModel alloc] init];
//                rankModel = [RankModel objectWithKeyValues:result];
//                [_dataArray addObjectsFromArray:rankModel.data];
//                
//                NSLog(@"%@",_dataArray);
//                [_tableview reloadData];
                rankModel = [[RankModel alloc] init];
                rankModel = [RankModel objectWithKeyValues:result];
                if (_dataArray.count==0) {
                    _dataArray = [[NSMutableArray alloc] init];
                    _dataArray=[NSMutableArray arrayWithArray:rankModel.data];
                    
                }else{
                    [_dataArray addObjectsFromArray:rankModel.data];
                }
                NSLog(@"%@",_dataArray);
                [_tableview reloadData];
                
                [_tableview.mj_header endRefreshing];
                [_tableview.mj_footer endRefreshing];

            }
            else{
                [_tableview.mj_header endRefreshing];
                [_tableview.mj_footer endRefreshing];
            }

        }];
    }else{
        SHOW_ALERT(@"提示", @"请等待定位，再登录", @"确定", nil, nil);
    }
}
- (void)headerRefreshing
{
    pageIndex = 0;
    [self creatThirdHttpRequest];
}

- (void)footerRefreshing
{
    if (_dataArray.count%10 == 0) {
        pageIndex ++;
        [self creatThirdHttpRequest];
    }else{
        [_tableview.mj_footer endRefreshing];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArray.count;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 110.f;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

        RankData *data=_dataArray[indexPath.row];
        InfoViewController *vc=[[InfoViewController alloc]init];
        vc.memberId=data.member_Id;
        [self.navigationController pushViewController:vc animated:YES];
        
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"Cell";
    
    AroudTeachersTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell=[[[NSBundle mainBundle] loadNibNamed:@"AroudTeachersTableViewCell" owner:self options:nil] objectAtIndex:0];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    RankData *data=_dataArray[indexPath.row];
    cell.lblTeaName.text=data.name;
    cell.lblZone.text=data.city;
    cell.lblTeachAge.text=data.seniority;
    cell.lblDistance.text=[NSString stringWithFormat:@"%@KM",data.distance];
    [cell.imgTeacherPhoto setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:data.imgUrl]] placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
        cell.imgTeacherPhoto.image=image;
    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
    }];
    cell.lblLeastPrice.text=[NSString stringWithFormat:@"￥%@元起",data.rockbottom_price];
    cell.lblTeseClasss.text=data.special_programs;
    cell.lblPersonalSign.text=data.signature;
    return cell;
}
- (IBAction)backAction:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
